package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.*;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.CheckGroupVO;
import com.itheima.health.vo.SetmealVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {


    @Autowired
    private OrderSettingService orderSettingService;

    @Autowired
    private MemberService memberService;

    @Override
    public Order add(Map map) {

        Order  order = new Order();
        String telephone  = (String)map.get("telephone");
        String orderDate  = (String)map.get("orderDate");
        String setmealId  = (String)map.get("setmealId");
        String orderType  = (String)map.get("orderType");
        order.setOrderStatus("未到诊");  //  已到诊  未到诊
        order.setSetmealId(Integer.parseInt(setmealId));
        order.setOrderType(orderType);
        order.setOrderDate(DateUtils.parseString2Date(orderDate,"yyyy-MM-dd"));

        String idCard  = (String)map.get("idCard");
        String sex  = (String)map.get("sex");
        String name  = (String)map.get("name");

         //  select  count(0) from t_ordersetting where  orderdate = ? and number > 已经预约数
       int count =  orderSettingService.isOrderOkByOrderDate(orderDate);
       if(count>0){
           //  当天没有预约满 。。。  当前用户是否是新还是老用户
          Member member=  memberService.findMemeberByTelephone(telephone);
            if(member!=null){
                  //    老会员    才会有重复预约的问题 ？   根据 预约日期和套餐id 和会员id  三者条件并列  存储结果  ： 预约过了！
                //  select count(1) from t_order where member_id = ? and orderdate = ? and setmeal_id = ?
                 int  isok =  baseMapper.hasOrderByOrderDateAndSetmealIdAndMemberId(orderDate,Integer.parseInt(setmealId),member.getId());
                 if(isok>0){
                     throw new RuntimeException("请勿重复预约");
                 }else {
                     //   可以预约 。。。   多表更新  insert  update
                     order.setMemberId(member.getId());
                     baseMapper.insert(order);
                     //  下单成功    t_order  insert  /  t_ordersetting  update  已经预约数+1
                     orderSettingService.updateReservationsByOrderDate(orderDate);
                 }
            }else{
                // 新会员   没有预约过。。。可以预约
                member = new Member();
                member.setRegTime(new Date(System.currentTimeMillis()));
                member.setPhoneNumber(telephone);
                member.setName(name);
                member.setIdCard(idCard);
                member.setSex(sex);
                memberService.save(member);
                //    录入会员数据   t_member  insert
                order.setMemberId(member.getId());
                baseMapper.insert(order);
                orderSettingService.updateReservationsByOrderDate(orderDate);
                //     下单成功    t_order  insert  /  t_ordersetting  update  已经预约数+1
            }
       }else{
           throw new RuntimeException("当天预约满，请择日再约！");
       }
        return order;
    }

    @Override
    public Map findOrderDetailByOrderId(int id) {
        return baseMapper.findOrderDetailByOrderId(id);
    }
}



